package ciel.image;

import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.components.visibleComponents.widgets.buttons.MTSvgButton;
import org.mt4j.input.gestureAction.InertiaDragAction;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.animation.Animation;
import org.mt4j.util.animation.AnimationEvent;
import org.mt4j.util.animation.IAnimationListener;
import org.mt4j.util.animation.MultiPurposeInterpolator;
import org.mt4j.util.math.Vector3D;

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import ciel.debugging.CielDebugger;
import ciel.res.CielSettings;
import ciel.scene.CielImageScene;
import ciel.utils.CielMath;

public class CielImage extends MTImage {

	private String note;
	public float currentRotation = 0;

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public CielImage(PApplet pApplet, PImage texture) {
		super(pApplet, texture);
		setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK );
		addGestureListener(DragProcessor.class, new InertiaDragAction(300, .97f, 17));
		note = "";
	}
	
	public void addButtons(PApplet pApplet) {
		MTSvgButton closeButton = new MTSvgButton(pApplet, "src/ciel/res/button_close_svg.svg");
		MTSvgButton editButton = new MTSvgButton(pApplet, "src/ciel/res/button_edit_svg.svg");
		
		editButton.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				TapEvent te = (TapEvent)ge;
				if(te.isTapped()) {
					((CielImageScene) getUserData("scene")).addAnnotator((((String)getUserData("name"))));
				}
				return false;
			}
		});
		
		closeButton.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				TapEvent te = (TapEvent)ge;
				if(te.isTapped()) {
					closeWithAnimation();
				}
				return false;
			}
		});
		
		float factor = CielMath.getScaleDimension(CielSettings.Scale.closeButton, closeButton.getWidthXYGlobal());
		closeButton.scale(factor, factor, factor, getCenterPointGlobal());
		editButton.scale(factor, factor, factor, getCenterPointGlobal());
		
		addChild(closeButton);
		addChild(editButton);
		
		float r = closeButton.getWidthXYRelativeToParent()/2;
		float x = getPosition(TransformSpace.GLOBAL).x + getWidthXY(TransformSpace.GLOBAL)/2;
		float y = getPosition(TransformSpace.GLOBAL).y - getHeightXY(TransformSpace.GLOBAL)/2;
		closeButton.setPositionGlobal(new Vector3D(x,y));
		y = getPosition(TransformSpace.GLOBAL).y + getHeightXY(TransformSpace.GLOBAL)/2;
		editButton.setPositionGlobal(new Vector3D(x,y));
	}
	
	private void closeWithAnimation() {
		float width = this.getWidthXY(TransformSpace.RELATIVE_TO_PARENT);
		Animation imageCloseAnimation = new Animation("Image Fade Out", new MultiPurposeInterpolator(width, 1, 300, 0.2f, 0.5f, 1), this);
		imageCloseAnimation.addAnimationListener(new IAnimationListener(){
			public void processAnimationEvent(AnimationEvent ae) {
				switch (ae.getId()) {
				case AnimationEvent.ANIMATION_STARTED:
				case AnimationEvent.ANIMATION_UPDATED:
					float currentVal = ((Animation) ae.getAnimation()).getInterpolator().getCurrentValue();
					setWidthXYRelativeToParent(currentVal);
					break;
				case AnimationEvent.ANIMATION_ENDED:
					//setVisible(false);
					((CielImageScene) getUserData("scene")).removeImageFromCanvas((((String)getUserData("name"))));
					break;	
				default:
					break;
				}
			}
		});

		imageCloseAnimation.start();
	}
	
	public void openWithAnimation() {
		Animation imageOpenAnimaion = new Animation("Image Fade In", new MultiPurposeInterpolator(1, 303, 300, 0.2f, 0.5f, 1), this);
		imageOpenAnimaion.addAnimationListener(new IAnimationListener(){
			public void processAnimationEvent(AnimationEvent ae) {
				switch (ae.getId()) {
				case AnimationEvent.ANIMATION_STARTED:
				case AnimationEvent.ANIMATION_UPDATED:
					float currentVal = ((Animation) ae.getAnimation()).getInterpolator().getCurrentValue();
					setWidthXYRelativeToParent(currentVal);
					break;
				case AnimationEvent.ANIMATION_ENDED:
					setVisible(true);
					break;	
				default:
					break;
				}
			}
		});
		imageOpenAnimaion.start();
	}
	
	@Override
	public void drawComponent(PGraphics g) {
		if(!CielImageScene.working) {
		super.drawComponent(g);
		
		Vector3D tmpVector = getCenterPointGlobal();
		if(tmpVector.x >= CielSettings.Dimensions.SCREEN_WIDTH) {
			setPositionGlobal(new Vector3D(0, tmpVector.y));
		} else if (tmpVector.x <= 0) {
			setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH, tmpVector.y));
		}
		if(tmpVector.y >= CielSettings.Dimensions.SCREEN_HEIGHT) {
			setPositionGlobal(new Vector3D(tmpVector.x, 0));
		} else if (tmpVector.y <= 0) {
			setPositionGlobal(new Vector3D(tmpVector.x, CielSettings.Dimensions.SCREEN_HEIGHT));
		}
		}
	}
	
	@Override
	public void rotateZ(Vector3D rotationPoint, float degree) {
		// TODO Auto-generated method stub
		super.rotateZ(rotationPoint, degree);
		currentRotation += degree;
		CielDebugger.log(""+currentRotation);
	}

}
